﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

public partial class KeyUnit_KeyUnitAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {







        if (!IsPostBack)
        {

            TabContainer1.ActiveTab = TabContainer1.Tabs[0];
            // TabContainer1.Tabs[1].Visible = false;
            ////得到登陆者所属支队或大队id
            string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            //User.Identity.Name
            //string DepartmentID = "1300100";
            //  1300100 第三位 0、1支队2大队5派出所
            //判断是大队还是支队,大队=显示派出所
            string group = DepartmentID;
            group = group.Substring(4, 1);
            //大队
            //if (group == "2")
            //    this.RadioButtonList2.Visible = true;
            //if (group == "0"||group=="1")
            //    RadioButtonList2.Visible = false;

            try
            {
                DBCls db = new DBCls();
                DataTable dt = new DataTable();
                //读取本用户所在大队的派出所列表
                //DataTable dt = db.QueryDataTable("select DepartmentID,FullName from PoliceStationDepartments where ParentID='" + DepartmentID + "' ", "PoliceStation");
                //if (dt.Rows.Count > 0)
                //{
                //    ddlPoliceStations.Items.Clear();
                //    for (int i = 0; i < dt.Rows.Count; i++)
                //    {
                //        ddlPoliceStations.Items.Add(new ListItem(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString()));
                //    }
                //}
                lblDWGM_Police.Visible = false;
                ddlPoliceStations.Visible = false;
                //读取重点单位类型
                dt.Clear();
                dt = db.QueryDataTable("select KeyUnitTypeID,KeyUnitTypeDescript from KeyUnitType  order by cast( keyunittypeid  as int) ", "KeyUnitType");
                if (dt.Rows.Count > 0)
                {
                    ddlUnitType.Items.Clear();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ddlUnitType.Items.Add(new ListItem(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString()));
                    }
                }
                //重点单位类型对照表
                string sql = "select KeyUnitTypeDescript,KeyUnitTypeName from KeyUnitType order by cast( keyunittypeid  as int) ";
                SqlDataReader sqlReader = db.SqlReader(sql);

                DataTable Dt = new DataTable();
                DataRow dr;
                Dt.Columns.Add("KeyUnitTypeDescript", typeof(string));    //第几类
                Dt.Columns.Add("KeyUnitTypeName", typeof(string));   //类型全称



                while (sqlReader.Read())
                {
                    dr = Dt.NewRow();
                    dr[0] = sqlReader[0].ToString();
                    dr[1] = sqlReader[1].ToString();

                    Dt.Rows.Add(dr);
                }
                sqlReader.Close();
                this.DataList1.DataSource = new DataView(Dt);
                this.DataList1.DataBind();

                //判断此支队大队是否已输入 重点单位总数
                //消防
                string sqlgetdid = "select * from KeyUnitCount where DepartmentID='" + DepartmentID + "'";
                DataTable dt2 = db.QueryDataTable(sqlgetdid, "DepartmentID");
                if (dt2.Rows.Count > 0)
                {
                    this.lblCount.Visible = false;
                    this.txtCount.Visible = false;
                }
                else
                {
                    this.lblCount.Visible = true;
                    this.txtCount.Visible = true;
                }
                //派出所
                string tdid = DepartmentID.Substring(0, 4) + "5" + DepartmentID.Substring(5, 2);
                sqlgetdid = "select * from KeyUnitCount where DepartmentID='" + DepartmentID + "'";
                dt2.Clear();
                dt2 = db.QueryDataTable(sqlgetdid, "Departm");
                if (dt2.Rows.Count > 0)
                {
                    this.lblCount.Visible = false;
                    this.txtCount.Visible = false;
                }
                else
                {
                    this.lblCount.Visible = true;
                    this.txtCount.Visible = true;
                }

                //图片类型
                //rblImgType.Enabled = false;
                for (int i = 0; i < rblImgType.Items.Count; i++)
                {
                    rblImgType.Items[i].Enabled = false;
                }
                rblImgType.Items[0].Enabled = true;

                //固定图片说明
                if (rblImgType.SelectedIndex == 0)
                {
                    txtDescript.Text = "单位的外观图";
                }

            }
            catch
            {
                Response.Write("<script language=javascript>alert('页面绑定信息出错，请刷新.')</script>");
            }
        }
    }
    //保存重点单位信息
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //得到登陆者所属支队或大队id
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        ////得到登陆者名称
        string username = User.Identity.Name;

        //string username = "liuzd";
        //string DepartmentID = "300100";

        string counts = txtCount.Text;//单位总数

        //派出所
        string police = string.Empty;
        if (ddlPoliceStations.Visible)
            police = ddlPoliceStations.SelectedValue.ToString();
        else
            police = "";

        string unitName = txtUnitName.Text;      //单位名称
        string state = RadioButtonList1.SelectedValue.ToString();   //是否完成
        string unitType = ddlUnitType.SelectedValue.ToString();     //单位类型
        string unitInfo = txtUnitInfo.Text;     //单位简介
        string info = txtPerformanceInfo.Text;  //完成情况

        DateTime dtime = DateTime.Now;



        try
        {
            if (CheckUnitNameIsSame(unitName))
            {
                Response.Write("<script language=javascript>alert('错误，此单位名称已存在！')</script>");
                return;
            }

            DBCls db = new DBCls();

            ////添加应完成总数
            //string idd = System.Guid.NewGuid().ToString();
            //string sql1 = "insert into KeyUnitCount values ('" + idd + "','" + DepartmentID + "'," + counts + ")";



            //添加重点单位
            string id = System.Guid.NewGuid().ToString();




            bool rsl = false;

            //存储添加单位信息sql语句
            string[] temp = new string[2];

            if (txtCount.Visible)
            {
                string idd = System.Guid.NewGuid().ToString();

                //消防
                if (RadioButtonList2.SelectedIndex == 0)
                {
                    //添加应完成总数                 
                    string sql1 = "insert into KeyUnitCount values ('" + idd + "','" + DepartmentID + "'," + counts + ")";
                    string sql2 = "insert into Performance values('" + id + "','" + username + "','" + police + "','" + unitName + "','" + unitInfo + "','" + state + "','" + info + "','" + dtime + "','" + DepartmentID + "','" + unitType + "')";
                    //rsl = db.ExcuteSql2(sql1, sql2);
                    temp[0] = sql1;
                    temp[1] = sql2;
                }
                else
                {
                    //添加应完成总数
                    string tdid = DepartmentID.Substring(0, 4) + "5" + DepartmentID.Substring(5, 2);
                    string sql1 = "insert into KeyUnitCount values ('" + idd + "','" + tdid + "'," + counts + ")";
                    string sql2 = "insert into Performance values('" + id + "','" + username + "','" + police + "','" + unitName + "','" + unitInfo + "','" + state + "','" + info + "','" + dtime + "','" + tdid + "','" + unitType + "')";
                    //rsl = db.ExcuteSql2(sql1, sql2);
                    temp[0] = sql1;
                    temp[1] = sql2;
                }
            }
            else
            {
                string sql2 = "insert into Performance values('" + id + "','" + username + "','" + police + "','" + unitName + "','" + unitInfo + "','" + state + "','" + info + "','" + dtime + "','" + DepartmentID + "','" + unitType + "')";
                //rsl = db.ExcuteSql1(sql2);
                temp[0] = sql2;
            }
            ViewState.Add("sql", temp);
            //if (rsl)
            //{
            //    Response.Write("<script language=javascript>alert('保存成功！')</script>");
            //记录单位id
            ViewState.Add("PerformanceID", id);
            TabContainer1.Tabs[1].Visible = true;
            TabContainer1.ActiveTab = TabContainer1.Tabs[1];
            //    return;
            //}
            //else
            //{
            //    Response.Write("<script language=javascript>alert('保存失败！请重新检查填写信息。" + rsl + "')</script>");
            //    return;
            //}
        }
        catch
        {
            Response.Write("<script language=javascript>alert('错误！请重新检查填写信息。')</script>");
            return;
        }
    }
    //保存图片和说明
    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        string PerformanceID = string.Empty;
        try
        {
            //读取重点单位id
            PerformanceID = ViewState["PerformanceID"].ToString();
        }
        catch
        {
            TabContainer1.ActiveTab = TabContainer1.Tabs[0];
            Response.Write("<script language=javascript>alert('请先保存重点单位信息。')</script>");
            return;
        }
        //保存图片
        string path = string.Empty;
        bool img = SaveImg("", out path);
        if (img)
        {
            try
            {
                DBCls db = new DBCls();
                string id = System.Guid.NewGuid().ToString();
                string sql = "insert into PerformancePic values ('" + id + "','" + PerformanceID + "','" + path + "','" + txtDescript.Text + "','" + rblImgType.SelectedValue.ToString() + "')";
                if (db.ExcuteSql1_Tran(sql))
                {
                    //lblMsg="单位概貌以上传图片1张"，大于3时，下一图片类型显示和当前可选
                    /*
                     * 
                     * 1.19号
                     * 
                     */
                    Response.Write("<script language=javascript>alert('图片保存成功。')</script>");
                    //重新绑定并显示图片和说明
                    BindDateList(PerformanceID);
                }
            }
            catch (Exception ex)
            {

                {
                    //删除图片 System.Web.HttpContext.Current.Request.MapPath("UpLoadImages/") + newName + fileExtension

                    string allPath = Request.MapPath("") + path;
                    try
                    {
                        File.Delete(allPath);
                    }
                    catch { }

                }
                Response.Write("<script language=javascript>alert('图片保存失败！请重新上传。')</script>");
            }
        }
        else
        {
            Response.Write("<script language=javascript>alert('图片格式不正确，请上传 *.bmp,*.jpg,*.jpeg 格式图片')</script>");
        }

    }
    /// <summary>
    /// 绑定显示图片和说明
    /// </summary>
    void BindDateList(string PerformanceID)
    {
        DBCls db = new DBCls();
        string sql = "select PerformancePicID,PictureUrl,PictureDescript,PictureType from PerformancePic where PerformanceID='" + PerformanceID + "' order by case PictureType when '单位概貌' then 1 when '检查和整改火灾隐患' then 2 when '扑救初期火灾' then 3 when '组织引导人员疏散逃生' then 4 when '消防安全知识宣传教育培训' then 5 end ";
        SqlDataReader sqlReader = db.SqlReader(sql);

        DataTable Dt = new DataTable();
        DataRow dr;
        Dt.Columns.Add("PerformancePicID", typeof(string));    //主键
        Dt.Columns.Add("PictureUrl", typeof(string));   //图片地址
        Dt.Columns.Add("PictureDescript", typeof(string));   //图片描述
        Dt.Columns.Add("PictureType", typeof(string));    //图片类型

        int gm = 0;
        int jc = 0;
        int pj = 0;
        int zz = 0;
        int xf = 0;

        while (sqlReader.Read())
        {
            btnOK.Enabled = false;

            dr = Dt.NewRow();

            string type = sqlReader[3].ToString();

            dr[0] = sqlReader[0].ToString();
            dr[1] = sqlReader[1].ToString();
            dr[2] = sqlReader[2].ToString();
            dr[3] = type;
            Dt.Rows.Add(dr);

            if (type.Contains("概貌"))
            {
                gm += 1;
                if (gm > 0)
                {
                    rblImgType.Items[1].Enabled = true;
                    rblImgType.SelectedIndex = 1;
                    
                    //消防
                    if (RadioButtonList2.SelectedIndex == 0)
                    {
                        txtDescript.Text = "人员进行防火检查、巡查的照片";
                    }
                    else
                    {
                        txtDescript.Text = "员工检查火灾隐患照片";
                    }

                    if (RadioButtonList1.SelectedIndex == 1)
                    {
                        btnOK.Enabled = true;
                    }
                }
            }
            else if (type.Contains("检查"))
            {
                jc += 1;

                switch (jc)
                {
                    case 1:
                        
                        //消防
                        if (RadioButtonList2.SelectedIndex == 0)
                        {
                            txtDescript.Text = "消防控制室或消防水泵房内标识照片，如单位无消防控制室或消防水泵房，可以用灭火设施器材标识照片";
                        }
                        else
                        {
                            txtDescript.Text = "灭火设施器材标识照片";
                        }
                        break;
                    case 2:
                         //消防
                        
                            txtDescript.Text = "防火检查、巡查记录照片";
                        
                        break;
                    case 3:
                        txtDescript.Text = "火灾隐患整改记录照片";
                        break;
                }


                //消防
                if (RadioButtonList2.SelectedIndex == 0)
                {

                    if (jc > 3)
                    {
                        rblImgType.Items[2].Enabled = true;
                        rblImgType.SelectedIndex = 2;
                        txtDescript.Text = "员工利用消火栓、灭火器等设施器材灭火或人员报警照片";
                    }
                }
                //派出所
                else
                {
                    if (jc > 1)
                    {
                        rblImgType.Items[2].Enabled = true;
                        rblImgType.SelectedIndex = 2;
                        txtDescript.Text = "在场所内设置模拟起火点，员工利用现场消火栓、灭火器等设施器材灭火的照片";
                    }
                }
            }
            else if (type.Contains("扑救"))
            {
                pj += 1;

                switch (pj)
                {
                    case 1:
                        txtDescript.Text = "灭火行动组利用本单位的消防设施、器材扑救火灾";
                        break;
                    case 2:
                        txtDescript.Text = "安全救护组负责协助抢救、护送受伤人员的照片";
                        break;
                    case 3:
                        txtDescript.Text = "灭火和应急疏散预案照片或灭火和应急预案演练流程图照片";
                        break;
                }

                //消防
                if (RadioButtonList2.SelectedIndex == 0)
                {

                    if (pj > 3)
                    {
                        rblImgType.Items[3].Enabled = true;
                        rblImgType.SelectedIndex = 3;
                        txtDescript.Text = "主要出口、疏散通道安全疏散标识照片";
                    }
                }
                //派出所
                else
                {
                    if (pj > 0)
                    {
                        rblImgType.Items[3].Enabled = true;
                        rblImgType.SelectedIndex = 3;
                        txtDescript.Text = "主要出口、疏散通道安全疏散标识照片";
                    }
                }


            }
            else if (type.Contains("组织"))
            {
                zz += 1;

                switch (zz)
                {
                    case 1:
                        txtDescript.Text = "单位在显著位置设置的疏散指示图照片";
                        break;
                    case 2:
                        txtDescript.Text = "引导人员现场人员疏散的照片";
                        break;
                    case 3:
                        txtDescript.Text = "灭火和应急疏散演练纪录照片或人员安全疏散组织程序图照片";
                        break;
                }

                //消防
                if (RadioButtonList2.SelectedIndex == 0)
                {

                    if (zz > 3)
                    {
                        rblImgType.Items[4].Enabled = true;
                        rblImgType.SelectedIndex = 4;
                        txtDescript.Text = "单位内部开展消防宣传教育培训的现场照片";
                    }
                }
                //派出所
                else
                {
                    if (zz > 0)
                    {
                        rblImgType.Items[4].Enabled = true;
                        rblImgType.SelectedIndex = 4;
                        txtDescript.Text = "“一懂三会”提示牌照片";
                    }
                }

            }
            else if (type.Contains("消防"))
            {
                xf += 1;

                switch (xf)
                {
                    case 1:
                         //消防
                        if (RadioButtonList2.SelectedIndex == 0)
                        {
                            txtDescript.Text = "利用展板、专栏、广播、电视、网络等形式开展消防宣传教育的照片";
                        }
                        else
                        {
                            txtDescript.Text = "员工“四个能力”学习照片";
                        }
                        break;
                    case 2:
                         //消防
                        if (RadioButtonList2.SelectedIndex == 0)
                        {
                            txtDescript.Text = "灭火设施器材使用标识照片";
                        }
                        else
                        {
                            txtDescript.Text = "派出所管理单位消防登记卡照片";
                        }
                        break;
                    case 3:
                        txtDescript.Text = "人员密集场所在主要出入口设置的“两书”、“三提示”照片或悬挂、张贴的消防宣传标识照片";
                        break;
                }

                //消防
                if (RadioButtonList2.SelectedIndex == 0)
                {

                    if (xf > 3)
                    {
                        btnOK.Enabled = true;
                    }
                }
                //派出所
                else
                {
                    if (xf > 2)
                    {
                        btnOK.Enabled = true;
                    }
                }
            }
        }
        string s = "已上传图片数：";
        lblGM.Text = s + gm;
        lblJC.Text = s + jc;
        lblPJ.Text = s + pj;
        lblZZ.Text = s + zz;
        lblXF.Text = s + xf;

        sqlReader.Close();
        this.DltList.DataSource = new DataView(Dt);
        this.DltList.DataBind();
    }
    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    private bool SaveImg(string name1, out string localPath)
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();

        bool fileOK = false;
        HttpPostedFile postedFile = files[0];

        string fileName, fileExtension;
        fileName = System.IO.Path.GetFileName(postedFile.FileName);
        if (fileName != "")
        {
            fileExtension = System.IO.Path.GetExtension(fileName);
            String[] allowedExtensions = { ".bmp", ".jpg", ".jpeg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension.ToLower() == allowedExtensions[i])
                {
                    fileOK = true;
                    break;
                }
            }
        }
        if (fileOK)
        {

            //Bitmap bm = new Bitmap(path);
            //本地image文件夹路径
            string sPath = System.Web.HttpContext.Current.Request.MapPath("UpLoadImages/");
            //文件扩展名
            fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
            //随机文件名
            Random rnd = new Random();
            string newName = DateTime.Now.ToString("yyyyMMddHHmmssff");
            localPath = "UpLoadImages/" + newName + fileExtension;
            // postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("UpLoadImages/") + newName + fileExtension);

            // Bitmap bmp = new Bitmap(postedFile.InputStream);   string newName = DateTime.Now.ToString("yyyyMMddHHmmssff");



            double width = 1024;
            double height = 768;
            //从文件取得图片对象
            System.Drawing.Image image = System.Drawing.Image.FromStream(postedFile.InputStream);
            System.Double NewWidth, NewHeight;

            if (image.Width > image.Height)
            {
                NewWidth = width;
                NewHeight = image.Height * (NewWidth / image.Width);
            }
            else
            {
                NewHeight = height;
                NewWidth = (NewHeight / image.Height) * image.Width;
            }

            if (NewWidth > width)
            {
                NewWidth = width;
            }
            if (NewHeight > height)
            {
                NewHeight = height;
            }

            //取得图片大小
            System.Drawing.Size size = new Size((int)NewWidth, (int)NewHeight);
            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空一下画布
            g.Clear(Color.White);
            //在指定位置画图
            g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
            new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
            System.Drawing.GraphicsUnit.Pixel);

            //保存高清晰度的缩略图
            bitmap.Save(System.Web.HttpContext.Current.Request.MapPath("UpLoadImages/") + newName + fileExtension, ImageFormat.Jpeg);

            // bmp.Save(System.Web.HttpContext.Current.Request.MapPath("UpLoadImages/") + newName + fileExtension, ImageFormat.Jpeg);


            return true;

        }
        else
        {
            //提示
            localPath = "";
            return false;
        }

    }

    public static void PicCut(string postImg, string saveImg, double width, double height)
    {
        //原始图片名称
        string originalFilename = postImg;
        //生成的高质量图片名称
        string strGoodFile = saveImg;


        //从文件取得图片对象
        // System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
        System.Drawing.Image image = System.Drawing.Image.FromFile(postImg);



        System.Double NewWidth, NewHeight;

        if (image.Width > image.Height)
        {
            NewWidth = width;
            NewHeight = image.Height * (NewWidth / image.Width);
        }
        else
        {
            NewHeight = height;
            NewWidth = (NewHeight / image.Height) * image.Width;
        }

        if (NewWidth > width)
        {
            NewWidth = width;
        }
        if (NewHeight > height)
        {
            NewHeight = height;
        }

        //取得图片大小
        System.Drawing.Size size = new Size((int)NewWidth, (int)NewHeight);
        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
        //新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //清空一下画布
        g.Clear(Color.White);
        //在指定位置画图
        g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
        new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
        System.Drawing.GraphicsUnit.Pixel);


        ///文字水印
        //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
        //System.Drawing.Font f=new Font("宋体",10);
        //System.Drawing.Brush b=new SolidBrush(Color.Black);
        //G.DrawString("myohmine",f,b,10,10);
        //G.Dispose();



        ///图片水印
        //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
        //Graphics a = Graphics.FromImage(bitmap);
        //a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

        //copyImage.Dispose();
        //a.Dispose();
        //copyImage.Dispose();


        if (postImg.Trim() == saveImg.Trim())
        {
            if (File.Exists(saveImg))
            {
                File.Delete(saveImg);
            }
        }

        //保存高清晰度的缩略图
        bitmap.Save(saveImg, System.Drawing.Imaging.ImageFormat.Jpeg);

        g.Dispose();
        image.Dispose();
        bitmap.Dispose();

    }




    protected void btnImgDel_Click(object sender, EventArgs e)
    {

    }
    protected void btnImgDel_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandArgument == null)
            return;
        string id = e.CommandArgument.ToString();
        DBCls db = new DBCls();
        try
        {
            string performanceid = ViewState["PerformanceID"].ToString();

            string sqls = "select picturetype from performancepic where performanceid='" + performanceid + "' and  picturetype = ( select picturetype from performancepic where performancepicid='" + id + "')";
            DataTable dt = db.QueryDataTable(sqls, "pics");

            //派出所
            if (RadioButtonList2.SelectedIndex == 1)
            {
                string pictype = "";
                if (dt.Rows.Count>0)
                {
                    pictype = dt.Rows[0][0].ToString();
                }


                if (pictype == "单位概貌")
                {
                    if (dt.Rows.Count < 2)
                    {
                        Response.Write("<script language=javascript>alert('图片类型 " + dt.Rows[0][0].ToString() + " 的图片不得少于1张，删除前请先上传足够的图片。')</script>");
                        return;
                    }
                }
                else if (pictype == "检查和整改火灾隐患")
                {
                    if (dt.Rows.Count < 3)
                    {
                        Response.Write("<script language=javascript>alert('图片类型 " + dt.Rows[0][0].ToString() + " 的图片不得少于2张，删除前请先上传足够的图片。')</script>");
                        return;
                    }
                }
                else if (pictype == "扑救初期火灾")
                {
                    if (dt.Rows.Count < 2)
                    {
                        Response.Write("<script language=javascript>alert('图片类型 " + dt.Rows[0][0].ToString() + " 的图片不得少于1张，删除前请先上传足够的图片。')</script>");
                        return;
                    }
                }
                else if (pictype == "组织引导人员疏散逃生")
                {
                    if (dt.Rows.Count < 2)
                    {
                        Response.Write("<script language=javascript>alert('图片类型 " + dt.Rows[0][0].ToString() + " 的图片不得少于1张，删除前请先上传足够的图片。')</script>");
                        return;
                    }
                }
                else if (pictype == "消防安全知识宣传教育培训")
                {
                    if (dt.Rows.Count < 4)
                    {
                        Response.Write("<script language=javascript>alert('图片类型 " + dt.Rows[0][0].ToString() + " 的图片不得少于3张，删除前请先上传足够的图片。')</script>");
                        return;
                    }
                }

            }

            else if (RadioButtonList2.SelectedIndex == 0)
            {

                if (dt.Rows.Count < 5)
                {
                    Response.Write("<script language=javascript>alert('图片类型 " + dt.Rows[0][0].ToString() + " 的图片不得少于4张，删除前请先上传足够的图片。')</script>");
                    return;
                }
            }
            dt.Clear();
            dt = db.QueryDataTable("select PictureUrl from performancepic where PerformancePicID='" + id + "'", "picpath");
            if (dt.Rows.Count > 0)
            {
                string path = dt.Rows[0][0].ToString();
                if (db.ExcuteSql1_Tran("delete PerformancePic where PerformancePicID='" + id + "'"))
                {

                    string allPath = Request.MapPath("") + "//" + @path;
                    try
                    {
                        File.Delete(allPath);
                    }
                    catch (Exception ex) { }

                    Response.Write("<script language=javascript>alert('删除成功。')</script>");
                    BindDateList(performanceid);
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('删除失败。')</script>");
        }
    }
    /// <summary>
    /// 查询是否存在同名重点单位。存在=true；不存在=false
    /// </summary>
    /// <param name="unitname"></param>
    /// <returns></returns>
    private bool CheckUnitNameIsSame(string unitname)
    {
        DBCls db = new DBCls();
        string sql = "select performanceid from performance where keyunitname = '" + unitname + "'";
        DataTable dt = db.QueryDataTable(sql, "issame");
        if (dt.Rows.Count > 0)
            return true;
        else
            return false;
    }

    protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        //得到登陆者所属支队或大队id
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        DBCls db = new DBCls();
        //消防
        if (RadioButtonList2.SelectedIndex == 0)
        {
            string sqlgetdid = "select * from KeyUnitCount where DepartmentID='" + DepartmentID + "'";
            DataTable dt2 = db.QueryDataTable(sqlgetdid, "DepartmentID");
            if (dt2.Rows.Count > 0)
            {
                this.lblCount.Visible = false;
                this.txtCount.Visible = false;
            }
            else
            {
                this.lblCount.Visible = true;
                this.txtCount.Visible = true;
            }
        }
        else
        {
            string sqlgetdid = "select * from KeyUnitCount where DepartmentID='" + DepartmentID + "01'";
            DataTable dt2 = db.QueryDataTable(sqlgetdid, "DepartmentID");
            if (dt2.Rows.Count > 0)
            {
                this.lblCount.Visible = false;
                this.txtCount.Visible = false;
            }
            else
            {
                this.lblCount.Visible = true;
                this.txtCount.Visible = true;
            }
        }

    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string[] temp = new string[2];
        try
        {
            //读取重点单位id
            temp = (string[])ViewState["sql"];
        }
        catch
        {
            Response.Write("<script language=javascript>alert('单位信息填写有错误，请重新检查后再次点击“ 下一步 --> 完成 ”')</script>");
        }
        bool rsl = false;
        DBCls db = new DBCls();

        try
        {
            if (temp[0] != null && temp[1] != null)
                rsl = db.ExcuteSql2(temp[0].ToString(), temp[1].ToString());
            else if (temp[0] != null)
                rsl = db.ExcuteSql1(temp[0].ToString());

            if (rsl)
            {
                Response.Write("<script language=javascript>alert('完成成功！')</script>");
            }
            else
            {
                //应该删除图片
                Response.Write("<script language=javascript>alert('完成失败.数据库错误，请重新检查后再次点击“ 下一步 --> 完成 ”。')</script>");
            }
        }
        catch
        {
            Response.Write("<script language=javascript>alert('完成失败！请重新检查后再次点击“ 下一步 --> 完成 ”。')</script>");
        }
    }
}
